<?php
$xajax->configure('javascript URI', "_classes/xajax/");
$xajax->registerFunction('ghi_nho');
$xajax->registerFunction('add_ghi_nho');
$xajax->registerFunction('change_status_dh');
$xajax->registerFunction('change_product');
$xajax->registerFunction('del_template');
$xajax->registerFunction('set_defualt_template');
$xajax->registerFunction('change_block_status');
$xajax->registerFunction('del_block');
$xajax->registerFunction('edit_lienketweb');
$xajax->registerFunction('del_lienket_web');
$xajax->registerFunction('del_logo');
$xajax->registerFunction('del_donhang');
$xajax->registerFunction('editTour');
$xajax->registerFunction('editPlaces');
$xajax->registerFunction('editCatHotel');
$xajax->registerFunction('del_image');
$xajax->registerFunction('actSaleHotel');
$xajax->registerFunction('del_lienhe');
$xajax->processRequest();

/*BEGIN*/
function del_lienhe($id){
    global $tb_fix, $anyDB;
    $id = (int)$id;
    $anyDB->query("DELETE FROM ".$tb_fix."_lienhe WHERE mlh_id='$id'");            
    $xajax = new xajaxResponse;
    $xajax->remove("ll_".$id);
    return $xajax;       
}
function actSaleHotel($hId,$hSale){
    global $tb_fix, $anyDB,$dir_tpl;
    $hId=(int)$hId;
    $hSale=(int)$hSale;
    $update = "";
    $div = "";
    if($hSale == 1){ // huy bo
        $update = "0";
        $div = "<a style='padding: 2px;' title='khuyến mại: không' href='javascript:;' onclick='actSale($hId,0)' class='btn_no_text btn ui-state-default ui-corner-all tooltip'>
                                        <img style='height: 16px; width: 16px;' class='ui-icon ui-icon-wrench' src='$dir_tpl/images/dis.png'/>
        </a>";
    }else{
        $update = "1";
        $div = "<a style='padding: 2px;' title='khuyến mại: đang hiển thị' href='javascript:;' onclick='actSale($hId,1)' class='btn_no_text btn ui-state-default ui-corner-all tooltip'>
                                        <img style='height: 16px; width: 16px;' class='ui-icon ui-icon-wrench' src='$dir_tpl/images/act.png'/>
        </a>";
    }
    $sql = "update ".$tb_fix."_hotels set hos_sale_off = $update where hos_id = '$hId'";
    $q = $anyDB->query($sql);
    $xajax = new xajaxResponse;
    if($q){
        $xajax->assign("khuyenmai_$hId","innerHTML","$div");
    }else{
        $xajax->alert("Có lỗi xảy ra, liên hệ webmaster !");
    }
    return $xajax;
}
function del_image($table,$select,$where,$id,$div_id_remove){
    global $tb_fix, $anyDB;
    $xajax = new xajaxResponse;    
    $sql = "select $select from ".$tb_fix."_$table where $where = '$id'";
    $q = $anyDB->query($sql);
    if($anyDB->numRows($q)>0){
        $r = $anyDB->getNext($q);
        $imgLink = $r[$select];
        @unlink($imgLink);
        $sql = "delete from ".$tb_fix."_$table where $where = '$id'";
        $q = $anyDB->query($sql);
        if($q){            
            $xajax->remove("$div_id_remove".$id);
            return $xajax;
        }
    }
    
}
function editCatHotel($catid,$catparentid){
    global $tb_fix, $anyDB,$temp, $dir_tpl;
    $sql = "select * from ".$tb_fix."_hotel_categorys where hca_id = $catid";
    $q = $anyDB->query($sql);
    $t = $anyDB->numRows($q);    
    $xajax = new xajaxResponse;
    
    if($t > 0){        
        $r = $anyDB->getNext();
        $tTitle = $r['hca_title'];
        $cDesc = $r['hca_desc'];
        $xajax->assign("row_title","innerHTML","Đang sửa Danh mục: <font color='red'>$tTitle</font>");
        $xajax->assign("c_title","innerHTML","<input type='text' value='$tTitle' id='hca_title' name='hca_title' class='field text small' />");
        $xajax->assign("c_desc","innerHTML","<input type='text' value='$cDesc' id='hca_des' name='hca_des' class='field text small' />");
        $xajax->assign("btn_submit","innerHTML","<input class='ui-state-default ui-corner-all' type='submit' name='add' value='Cập nhật'/> <input type='reset' value='Hủy bỏ' class='ui-state-default ui-corner-all' onclick='window.location.reload(true)' />");
        $xajax->script("scrollWin()");
        return $xajax;
    }
}
function editPlaces($pid){
    global $tb_fix, $anyDB,$temp, $dir_tpl;
    $sql = "select * from ".$tb_fix."_tour_khoihanh where tkh_id = $pid";
    $q = $anyDB->query($sql);
    $t = $anyDB->numRows($q);    
    $xajax = new xajaxResponse;
    
    if($t > 0){        
        $r = $anyDB->getNext();
        $tID    = $r['tkh_id'];
        $tTitle = $r['tkh_name'];
        $tOrder = $r['tkh_order'];
        $xajax->assign("row_title","innerHTML","Đang sửa điểm khởi hành: <font color='red'>$tTitle</font>");
        $xajax->assign("edit","innerHTML","<input type='hidden' value='1' id='isEdit' name='isEdit'  />");
        $xajax->assign("CatId","innerHTML","<input type='hidden' value='$tID' id='catId' name='catId'  />");
        $xajax->assign("tn_name","innerHTML","<input type='text' value='$tTitle' id='cat_title' name='cat_title' size='30' />");        
        $xajax->assign("tn_order","innerHTML","<input type='text' value='$tOrder' id='cat_order' name='cat_order' size='9' />");
        $xajax->assign("btn_submit","innerHTML","<input class='ui-state-default ui-corner-all' type='submit' name='add' value='Cập nhật'/> <input type='reset' value='Hủy bỏ' class='ui-state-default ui-corner-all' onclick='window.location.reload(true)' />");
        $xajax->script("scrollWin()");
        return $xajax;
    }
}
function editTour($rTour,$tourId){
    global $tb_fix, $anyDB,$temp, $dir_tpl;
    $sql = "select tme_title from ".$tb_fix."_tour_menu where tme_id = $tourId";
    $q = $anyDB->query($sql);
    $t = $anyDB->numRows($q);    
    $xajax = new xajaxResponse;
    
    if($t > 0){        
        $r = $anyDB->getNext();
        $tTitle = $r['tme_title'];
        $xajax->assign("row_title","innerHTML","Đang sửa Tour: <font color='red'>$tTitle</font>");
        if($rTour == 1){
            $xajax->assign("nn_id","innerHTML","<input type='text' value='$tTitle' id='tour_title_nn' name='tour_title_nn' class='field text small' />");               
        }else{
            $xajax->assign("tn_id","innerHTML","<input type='text' value='$tTitle' id='tour_title_tn' name='tour_title_tn' class='field text small' />");               
        }
        $xajax->assign("btn_submit","innerHTML","<input class='ui-state-default ui-corner-all' type='submit' name='add' value='Cập nhật'/> <input type='reset' value='Hủy bỏ' class='ui-state-default ui-corner-all' onclick='window.location.reload(true)' />");
        $xajax->script("scrollWin()");
        return $xajax;
    }
}
function del_donhang($id){
    global $tb_fix, $anyDB;
    $dhaid = (int)$id;
            $anyDB->query("DELETE FROM ".$tb_fix."_customer WHERE cus_id=(select cus_id from ".$tb_fix."_don_hang where dha_id = $dhaid)");
            $anyDB->query("DELETE FROM ".$tb_fix."_don_hang WHERE dha_id='$dhaid'");
            $anyDB->query("DELETE FROM ".$tb_fix."_thanh_toan_dh WHERE dha_id='$dhaid'");
            $anyDB->query("DELETE FROM ".$tb_fix."_chi_tiet_dh WHERE dha_id='$dhaid'");
            
     $xajax = new xajaxResponse;
     $xajax->remove("tr_dha_".$dhaid);
     return $xajax;       
}
function del_logo($id){
    global $tb_fix, $anyDB;
    $sql = "select logo_img from ".$tb_fix."_logos where logo_id = $id";
    $q = $anyDB->query($sql);
    $r = $anyDB->getNext($q);
    $link_img = $r['logo_img'];
    unlink($link_img);
    $anyDB->query("delete from ".$tb_fix."_logos where logo_id = $id");
    $xajax = new xajaxResponse;
    $xajax->remove("log_".$id);
    return $xajax;
}
function del_lienket_web($id){
    global $tb_fix, $anyDB;
    $id=(int)$id;
    $sql = "delete from ".$tb_fix."_lienketweb where lkw_id = $id";
    $xajax = new xajaxResponse;
    if($anyDB->query($sql)){
        $xajax->remove("web_tt_".$id);
    }else{
        $xajax->alert("Không thể xóa liên kết website này!");
    }
    return $xajax;
}
function edit_lienketweb($id){
    global $tb_fix, $anyDB;
    $id=(int)$id;
    $sql = "select * from ".$tb_fix."_lienketweb where lkw_id = $id";
    $q = $anyDB->query($sql);
    $r = $anyDB->getNext($q);
    $name = $r['lkw_name'];
    $link = $r['lkw_link'];
    $xajax = new xajaxResponse;
    $xajax->assign("web_title","innerHTML","<strong>Chỉnh sủa liên kết website</strong>");
    $xajax->assign("web_id","innerHTML","<input name='lkw_id' id='lkw_id' type='hidden' value='".$id."' />");
    $xajax->assign("web_name","innerHTML","<input name='lkw_name' id='lkw_name' type='text' value='".$name."' size='55'/>");
    $xajax->assign("web_link","innerHTML","<input name='lkw_link' id='lkw_link' type='text' value='".$link."' size='55'/>");
    $xajax->assign("web_but","innerHTML","<input class='ui-state-default ui-corner-all' type='submit' name='edit' value='Hoàn Thành'/> <input type='reset' value='Hủy bỏ' class='ui-state-default ui-corner-all' onclick=redirect('admin.php?obj=LienKetWeb') />");
    return $xajax;
}
function del_block($bloid){
    global $tb_fix, $mysql;
    $sql = "select * from ".$tb_fix."_blocks where blo_id = $bloid";
    $q = $mysql->query($sql);
    if($q){
        $r = $mysql->fetch_array($q);
        $path = $r['blo_path'];
        $path_blog = "./_blocks/$path";
        $files = glob("$path_blog/*");
        foreach ($files as $file) // xoa file
        {
            unlink($file);
        }
        $path_temp = temp_default()."/blocks/$path"; // thu muc chua temp block, sua lai folder temp mac dinh tu database
        $files_1 = glob("$path_temp/*");
        foreach ($files_1 as $file1) // xoa file
        {
            unlink($file1);
        }
        $xajax = new xajaxResponse;
        if(!rmdir($path_blog)){
            $xajax->script("alert('Không xóa được thư mục block')");
        }
        if(!rmdir($path_temp)){
            $xajax->script("alert('Không xóa được thư mục template block')");
        }
        $q = $mysql->query("delete from ".$tb_fix."_blocks where blo_id = $bloid");
        if(!$q){
            $xajax->script("alert('Không xóa được dữ liệu block trong database')");
        }
        $xajax->remove("row_".$bloid);
        return $xajax;    
    }
}
function change_block_status($blogid,$status){
    global $tb_fix, $mysql, $dir_tpl;
    $sql = "update ".$tb_fix."_blocks set blo_status = $status where blo_id = $blogid";
    $mysql->query($sql);
    $xajax = new xajaxResponse;
    if($status == 0){
        $xajax->assign("block_".$blogid,"innerHTML","<img src=\"".$dir_tpl."/images/light2.png\" title=\"ẩn block này\" style=\"cursor: pointer;\" onclick=\"confirm_block_status($blogid,1)\" />");    
    }else{
        
        $xajax->assign("block_".$blogid,"innerHTML","<img src=\"".$dir_tpl."/images/light1.png\" title=\"hiển thị block này\" style=\"cursor: pointer;\" onclick=\"confirm_block_status($blogid,0)\" />");
    }
    
    return $xajax;
}
function set_defualt_template($tpl_id){
   global $tb_fix, $mysql;     
   $q = $mysql->query("update ".$tb_fix."_templates set tpl_default = 0 where tpl_default = 1");
   if($q){
        $mysql->query("update ".$tb_fix."_templates set tpl_default = 1 where tpl_id = $tpl_id"); 
   }
   $xajax = new xajaxResponse;
   $xajax->script("window.location='?obj=Templates'");
   return $axjax;
}
function del_template($tpl_id){
    global $tb_fix, $mysql;
    $sql = "select tpl_position from ".$tb_fix."_templates where tpl_id = $tpl_id";
    $r = $mysql->fetch_array($mysql->query($sql));
    $position = $r['tpl_position'];
    $pos = explode("|",$position);
    for($i=0;$i<count($pos);$i++){
        $_pos = $pos[$i];
        $sql = "delete from ".$tb_fix."_modules where tpl_position = '$_pos'";
        $mysql->query($sql);
    }
    $mysql->query("delete from ".$tb_fix."_templates where tpl_id = $tpl_id");
    $xajax = new xajaxResponse();
    $xajax->remove("row_".$tpl_id);
    return $xajax;
}
function change_product($proid,$status,$from){
    global $tb_fix, $mysql;
    isset($from) ? $sql_from = "$from" : $sql_from = "";
    $sql = "update ".$tb_fix."_product set $sql_from = $status where pro_id = $proid";
    $mysql->query($sql);
    $objResponse = new xajaxResponse();
    if($status == 1){
        if($sql_from == 'pro_focus') $title = 'Loại khỏi danh sách sản phầm Nổi bật';
        if($sql_from == 'pro_sale') $title = 'Loại khỏi danh sách sản phẩm bán chạy nhất';
        if($sql_from == 'pro_new') $title = 'Loại khỏi danh sách sản phẩm mới xuất hiện';
        if($sql_from == 'pro_offer') $title = 'Loại khỏi danh sách sản phẩm khuyễn mãi';
        $objResponse->assign($sql_from."_".$proid,"innerHTML","<a href='javascript:;' onclick='xajax_change_product($proid,0,\"$from\")'><img src='_images/activate.png' border='0' title='".$title."'></a>");    
    }else{
        if($sql_from == 'pro_focus') $title = 'Đưa vào danh sách sản phầm Nổi bật';
        if($sql_from == 'pro_sale') $title = 'Đưa vào danh sách sản phẩm bán chạy nhất';
        if($sql_from == 'pro_new') $title = 'Đưa vào danh sách sản phẩm mới xuất hiện';
        if($sql_from == 'pro_offer') $title = 'Đưa vào danh sách sản phẩm khuyễn mãi';
        $objResponse->assign($sql_from."_".$proid,"innerHTML","<a href='javascript:;' onclick='xajax_change_product($proid,1,\"$from\")'><img src='_images/inactive.png' border='0' title='".$title."'></a>");
    }
    return $objResponse;
}
function ghi_nho($userid,$action=''){
    global $tb_fix, $mysql;
    $url = "?".$_SERVER['QUERY_STRING'];
    $objResponse = new xajaxResponse();
    if($action == 'edit'){
        $sql = "select * from ".$tb_fix."_ghi_nho where gnh_user_id = $userid";
        $r = ($mysql->fetch_array($mysql->query($sql)));
        $ndung = $r['gnh_noi_dung'];
        $gnh_id = $r['gnh_id'];
        $objResponse->assign('gn_view','innerHTML','<textarea name="txtGhinho" id="txtGhinho" rows="2" style="width: 96%;height: 200px;">'.$ndung.'</textarea>');
        $objResponse->assign('inpGhinho','innerHTML','<input type="button" onclick="xajax_add_ghi_nho(xajax.getFormValues(\'frmGhinho\'),\'edit\','.$gnh_id.')" class="btn_no_text btn ui-state-default ui-corner-all tooltip" value="Lưu" style="float: none;">');
        $objResponse->script("document.getElementById('txtGhinho').focus()");    
    }
     else if($action == 'del'){
            $sql = "delete from ".$tb_fix."_ghi_nho";
            $q = $mysql->query($sql);
            $objResponse->script("window.location='$url'");
        }
    else{
        $objResponse->assign('ghi_nho','innerHTML','<textarea name="txtGhinho" id="txtGhinho" rows="2" style="width: 96%;height: 200px;"></textarea>');
        $objResponse->assign('inpGhinho','innerHTML','<input type="button" onclick="xajax_add_ghi_nho(xajax.getFormValues(\'frmGhinho\'))" class="btn_no_text btn ui-state-default ui-corner-all tooltip" value="Lưu" style="float: none;">');
        $objResponse->script("document.getElementById('txtGhinho').focus()");    
    }
    return $objResponse;
}
function add_ghi_nho($form,$action='',$gnh_id=0){
    global $tb_fix, $mysql;
    $url = "?".$_SERVER['QUERY_STRING'];
    $uid = $form['user_id'];
    $noidung = stripslashes(urldecode($form["txtGhinho"]));
    $objResponse = new xajaxResponse();
    
    
    if(empty($noidung)){
        $objResponse->alert('Chưa nhập nội dung ghi nhớ!');
        $objResponse->script("document.getElementById('txtGhinho').focus()");       
    }else{
        $time = time();
        if($action == 'edit' && $gnh_id != 0){
            $sql = "update ".$tb_fix."_ghi_nho set gnh_noi_dung = '$noidung',gnh_thoi_gian = '$time' where gnh_id = $gnh_id";
            $mes = 'Đã sửa ghi nhớ!';
        }
       
        else{
            $sql = "insert into ".$tb_fix."_ghi_nho values (0,$uid,'$noidung',$time)";
            $mes = 'Đã thêm ghi nhớ!';    
        }
        $q=$mysql->query($sql);
        if($q){
           $objResponse->alert($mes);
           $objResponse->script("window.location='$url'");
        }        
        else{
           $objResponse->alert('Không thể thêm ghi nhớ, kiểm tra lại!'); 
        }    
    }    
    return $objResponse;
}
function change_status_dh($dha_id,$status,$from=''){
    global $mysql,$tb_fix;
    if($status == 1){
        $sta_sql = 1;
        $img = show_img_status(1,$dha_id);        
    }else{
        $sta_sql = 0;
        $img = show_img_status(0,$dha_id);
    }
    $sql = "update ".$tb_fix."_don_hang set dha_status = $sta_sql where dha_id = $dha_id";
    $mysql->query($sql);
    $objResponse = new xajaxResponse();
    if($from == 'view'){
        $objResponse->alert('Đã cập nhật trạng thái đơn đặt hàng!');
        $objResponse->assign('dh_status','innerHTML','<font color="green">Đã xử lý</font>');
        $objResponse->remove('txtSubmit');   
    }else{
        $objResponse->assign("status_$dha_id",'innerHTML',$img);    
    }
    return $objResponse; 
}
function show_img_status($status,$id){
    if($status == 0){
        $html = "<img src='_images/inactive.png' class='tooltip' title='đơn đặt hàng chưa hoàn thành' onclick='xajax_change_status_dh($id,1);' />";
    }else{
        $html = "<img src='_images/activate.png' class='tooltip' title='đơn đặt hàng đã hoàn thành' onclick='xajax_change_status_dh($id,0);' />";
    }
    return $html;
}

?>